import React, { Component } from 'react'
import { NavBar, Icon } from 'antd-mobile'
import { withRouter, Redirect } from 'react-router-dom'
import api from '@UTILS/api'
import store from 'store'
import helper from '@UTILS/helper'
import config from '@UTILS/config'

class Header extends Component {

  constructor(props) {
    super(props)
    this.state = {
      title: '',
      needLogin: false
    }
  }

  componentDidMount() {
    this.readyWxConfig()
  }

  wxConfig() {
    const { title } = this.state
    if (title !== '首页' && title !== '商品详情' && title !== config.thingName && title !== '分享赚钱' && title !== '新人专区') {
      return
    }
    let domain_url = window.location.origin
    api.getWxConfig({ url: window.location.href }, {
      success: (data) => {
        window.wx.config(data.config)
        window.wx.ready(function () {   //需在用户可能点击分享按钮前就先调用

          let title = '点击免费领取商品，本人亲测有效'
          let desc = config.name +'，商品每日上新，无需任何费用，亲测简单就可以得到免费商品'
          let link = domain_url + '/activities'
          if (store.get('user')) {
            link = domain_url + '/activities?invitation=' + helper.getInvitationCode()
          }
          let logosrc = domain_url + '/logo.png'

          window.wx.updateAppMessageShareData({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: logosrc, // 分享图标
          }, function (res) {
            //这里是回调函数
          })


          window.wx.updateTimelineShareData({
            title: title, // 分享标题
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: logosrc, // 分享图标
          }, function (res) {
            //这里是回调函数
          })

          window.wx.onMenuShareTimeline({
            title: title, // 分享标题
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: logosrc, // 分享图标
            success: function () {
              // 用户点击了分享后执行的回调函数
            }
          })
          window.wx.onMenuShareAppMessage({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: logosrc, // 分享图标
            success: function () {
              // 用户点击了分享后执行的回调函数
            }
          });
        })
      }
    })
  }

  componentWillReceiveProps() {
    this.readyWxConfig()
    return true
  }



  render() {
    const { title, needLogin } = this.state

    if (needLogin) {
      return (
        <Redirect to="/login" />
      )
    }
    if(title === '首页' || title === config.thingName ){
      return ''
      //window.location.reload(true); 
    }
    else if ( title === '商品详情' || title === '分享' || title === '注册成功' || title === '成为代言人' || title === config.goldCoin+'抽奖') {
      return ''
    } else if (title === '绑定支付宝' || title === '申请流程' || title === '试用详情'|| title === '意见反馈' || title === '修改订单信息' || title === '个人资料' || title === '提现申请' || title === '帮助与客服' || title==='签到' || title==='公告中心'|| title === '用户协议' || title === '我的二维码' || title === '分享' || title === '我的'+config.goldCoin ) {
      return (
        <div style={{ position: 'fixed', top: 0, zIndex: 8999, width: '100%' }}>
          <NavBar
            style={{ height: 44, color: '#3A3A3A', fontSzie: 18, boxShadow: '0 0 5px rgba(0,0,0,0.2)' }}
            mode="light"
            icon={<Icon color='#BFBFBF' type="left" />}
            onLeftClick={() => this.props.history.goBack()}
          >{title}</NavBar>
        </div>
      )
    } else {
      return (
        <NavBar
          style={{ height: 44, color: '#3A3A3A', fontSzie: 18, boxShadow: '0 0 5px rgba(0,0,0,0.2)' }}
          mode="light"
          icon={<Icon color='#BFBFBF' type="left" />}
          onLeftClick={() => this.props.history.goBack()}
        // rightContent={
        //   title === '登录' ?
        //     <span style={{ fontSize: 13, color: '#FF5A3F' }} onClick={()=>this.gotoRegister()}>新用户注册</span>
        //     : null}
        >{title}</NavBar>
      )
    }
  }

  // gotoRegister() {
  //   if (this.props.location.search === '') {
  //     this.props.history.push('/register')
  //   } else {
  //     this.props.history.push('/register?jump=1')
  //   }
  // }

  // 处理导航标题
  showTitle() {
    const { routerConfig } = this.props
    const path = this.props.match.path
    for (let i = 0; i < routerConfig.length; i++) {
      const element = routerConfig[i]
      if (element.path === path) {
        return element.title;
      }
    }
  }

  readyWxConfig() {
    let title = this.showTitle()
    if (!store.get('user') && title !== '首页' && title !== '登录' && title !== '微信登录' && title !== '微信登录绑定' && title !== '注册' && title !== '找回密码' && title !== '商品详情' && title !== '搜索' && title !== config.thingName && title !== '新人专区' && title !== '中奖秘籍' && title !== '用户协议' && title !== '折扣卖专区') {
      this.setState({
        needLogin: true
      })
    }
    this.setState({
      title: this.showTitle()
    }, () => {
      // 微信配置
      let ua = navigator.userAgent.toLowerCase()
      ua.match(/micromessenger/i) && this.wxConfig()
    })
  }
}

export default withRouter(Header)
